<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索 -->
          <el-input placeholder="请输入内容" class="input-with-select" v-model="info.query" clearable @clear='getGoodList'
            @keyup.enter.native="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <!-- 添加商品按钮 -->
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间">
          <template v-slot='{row}'>
            {{row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot='{row}'>
            <!-- 修改 -->
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <!-- 删除 -->
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeGood(row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="info.pagenum"
        :page-sizes="[5, 10, 15, 20 , 50]" :page-size="info.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品列表数据
      goodList: [],
      // 获取商品列表请求参数
      info: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 20
      },
      // 商品总数
      total: 0
    }
  },
  methods: {
    // 删除商品
    removeGood (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) return this.$message.error('删除商品失败')
        this.$message.success('删除商品成功')
        this.getGoodList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 监听分页状态改变
    handleCurrentChange (newNum) {
      this.info.pagenum = newNum
      this.getGoodList()
    },
    handleSizeChange (newSize) {
      this.info.pagesize = newSize
      this.getGoodList()
    },
    // 获取商品列表数据
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', { params: this.info })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.goodList = res.data.goods
      this.total = res.data.total
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getGoodList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.el-table {
  margin: 15px 0;
}
</style>
